/*
 *    Copyright (c) 2018-2025, nart All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: nart
 */

package com.nai365.nart.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.nai365.nart.common.core.util.R;
import com.nai365.nart.common.log.annotation.SysLog;
import com.nai365.nart.common.security.util.SecurityUtils;
import com.nai365.nart.product.entity.SpuRemind;
import com.nai365.nart.product.service.SpuRemindService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 藏品购买提醒
 *
 * @author 罗光林
 * @date 2022-07-18 11:57:18
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/spu/remind")
@Api(value = "spu remind", tags = "藏品购买提醒管理")
public class SpuRemindController {

	private final SpuRemindService spuRemindService;


	/**
	 * 根据用户和产品查询提醒
	 *
	 * @param spuId 分页对象
	 * @return R<SpuRemind>
	 */
	@ApiOperation(value = "根据用户和产品查询提醒", notes = "根据用户和产品查询提醒")
	@GetMapping("/spu/{spuId}")
	public R<SpuRemind> queryBySpuId(@PathVariable(value = "spuId") Long spuId) {
		LambdaQueryWrapper<SpuRemind> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(SpuRemind::getSpuId, spuId).eq(SpuRemind::getUserId, SecurityUtils.getUser().getId());
		List<SpuRemind> list = spuRemindService.list(queryWrapper);
		if (list != null && !list.isEmpty()) {
			return R.ok(list.get(0));
		} else {
			SpuRemind spuRemind = new SpuRemind();
			spuRemind.setUserId(SecurityUtils.getUser().getId());
			spuRemind.setSpuId(spuId);
			spuRemindService.save(spuRemind);
			return R.ok(spuRemind);
		}
	}

	/**
	 * 保存藏品购买提醒
	 *
	 * @param spuRemind 保存藏品购买提醒
	 * @return R<SpuRemind>
	 */
	@ApiOperation(value = "保存藏品购买提醒", notes = "保存藏品购买提醒")
	@SysLog("新增藏品购买提醒")
	@PostMapping
	public R<SpuRemind> save(@RequestBody SpuRemind spuRemind) {
		spuRemind.setUserId(SecurityUtils.getUser().getId());
		if (spuRemind.getId() != null && spuRemind.getId() != 0) {
			spuRemindService.updateById(spuRemind);
		} else {
			spuRemind.setId(null);
			spuRemindService.save(spuRemind);
		}
		return R.ok(spuRemind, "设置提醒成功!");
	}

}
